import scrapy
import json,jsonpath
import re
from scrapy import FormRequest,Request
from ..user_setting import username,password
from ..shopItems import ShopItem
from ..reviewItems import reviewItem
class taobao(scrapy.Spider):
    name = 'taobao'
    shop_url = 'https://s.taobao.com/search?q={q}&bcoffset=0&ntoffset=0&s={s}'
    login_url = 'https://login.taobao.com/member/login.jhtml'
    #review_url = "https://rate.taobao.com/feedRateList.htm?auctionNumId={itemid}&userNumId={spuId}&currentPageNum={page}&pageSize=20&callback=json"
    #review_url = "https://rate.tmall.com/list_detail_rate.htm?itemId={itemid}&spuId={spuId}&sellerId={sellerId}&order=3&currentPage={page}"
    #review_url = "https://rate.tmall.com/list_detail_rate.htm"
    def __init__(self,keyworld):
        self.keyworld = keyworld
        self.start_urls = ['https://s.taobao.com/search?q={q}&bcoffset=0&ntoffset=0&s={s}'.format(q=keyworld,s='0')]
    # 商品抓取
    def parse(self, response):
        html = response.text
        nid = re.findall('"nid":"(.*?)"',html)# 商品id
        if(len(nid) == 0):
            return 
        user_id = re.findall('"user_id":"(.*?)"',html)# 店铺id
        name_pat = re.findall('"raw_title":"(.*?)"',html)# 商品名称
        nick_pat = re.findall('"nick":"(.*?)"',html)# 店铺名称
        price_pat = re.findall('"view_price":"(.*?)"',html)# 商品价格
        sales_pat = re.findall('"view_sales":"(.*?)"',html)# 付款人数
        city_pat = re.findall('"item_loc":"(.*?)"',html)# 店铺所在地
        pic_url = re.findall('"pic_url":"(.*?)"',html)#商品的主图
        for i in range(len(name_pat)):
            item = ShopItem()
            item['nid']=nid[i]
            item['user_id']=user_id[i]
            item['name_pat']=name_pat[i]
            item['price_pat']=price_pat[i]
            item['sales_pat']=sales_pat[i]
            item['nick_pat']=nick_pat[i]
            item['city_pat']=city_pat[i]
            item['pic_url'] = pic_url[i]
            item['f'] = 'shop'
            yield item
            # yield Request(self.review_url.format(itemid=nid[i],spuId=user_id[i],sellerId=user_id[i],page='1'),callback=self.parse_review)
        # for i in range(1,1):
        current_url = str(response.request.url)
        current_page = int(re.findall('&s=(\d+)',current_url)[0])
        yield Request(self.shop_url.format(q=self.keyworld,s=str(current_page+48)),callback=self.parse)
        #return 
        
    # def parse_review(self,response):
    #     JSONP = str(response.text).replace("json","")
    #     Json =re.findall(r'.*?({.*}).*',JSONP,re.S)[0]
    #     j =  json.loads(Json)


        # rateList =  j['rateDetail']['rateList']
        # current_url = str(response.request.url)
        # while(len(rateList) > 0):
        #      # 评论项
        #     for i in rateList:
        #         review_item = reviewItem()
        #         review_item['nid']=re.findall("itemId=(.*?)&",current_url)[0]
        #         review_item['user_id']=re.findall("spuId=(.*?)&",current_url)[0]
        #         review_item['id'] = i['id'] #id
        #         review_item['rateContent'] = i['rateContent'] # 评论的内容
        #         review_item['rateDate'] = i['rateDate'] # 评论时间
        #         review_item['auctionSku'] = str(i['auctionSku'])# 购买的物品类型
        #         review_item['f'] = 'review'
        #         yield review_item

        #     # 下一页链接
        #     current_page = j['rateDetail']['paginator']['page'] # 当前页数
        #     total_pages = j['rateDetail']['paginator']['lastPage'] #总页数
        #     next_page = int(current_page)  + 1
        #     if(next_page > total_pages):
        #         return
        #     next_url = re.sub("&currentPage=(.+)",'&currentPage='+str(next_page),current_url)
        #     # page = int(response.meta['page'])+1
        #     # print(response.meta['nid'])
        #     # print(response.meta['user_id'])
        #     # print(meta['page'])
        #     yield Request(next_url,callback=self.parse_review)
            
    # 登陆的请求构造
    def start_requests(self):
         yield scrapy.FormRequest(
             self.login_url,
             formdata={
                 "TPL_password":password,
                 "TPL_username":username,
             },
             callback=self.start_url
         )
    
    # 登陆的验证
    def start_url(self,response):
       yield from super().start_requests()

    

        

    